<?php
namespace app\index\model;

use think\Model;
use think\DB;

class Comments extends Model
{
    // 获取评论列表
    public function getCommentsList($vid,$page){
        return DB::name('comments')->page("$page,20")->where('vid',$vid)->select();
    }

    //获取某视频的评论总条数
    public function getCommentsListCount($vid){
        return DB::name('comments')->where('vid',$vid)->count();
    }

    // 写评论
    public function putComments($vid,$comment,$uid){
        //判断视频是否存在，不存在则返回0
        $vid_res = DB::name('video_list')->where('vid',$vid)->find();
        if(empty($vid_res)){
            return 0;
        }

        //同一用户同一视频，评论时间必须超过120s
        $uid_last_comments = [
            'vid' => $vid,
            'uid' => $uid,
        ];
        $last_comments_time = DB::name('comments')->where($uid_last_comments)->order('id desc')->find();

        if(time() - $last_comments_time['commentstime'] <= 120){
            return 0;
        }

        $map = [
            'vid' => $vid,
            'uid' => $uid,
            'contents' => $comment,
            'commentstime' => time(),
        ];
        Db::name('comments')->insert($map);
        return $this->getLastInsID();

    }

}